package br.com.datasun.biblioteca.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.datasun.biblioteca.dao.factory.ConnectionFactory;
import br.com.datasun.biblioteca.entities.TipoUsuario;
import br.com.datasun.biblioteca.exceptions.DAOException;

public class UsuarioDAO {
	
	public TipoUsuario getTipoUsuario(String login, String senha) throws DAOException{
		TipoUsuario resultado = null;
		Connection connection = null;
		PreparedStatement preparedStatement   = null;
		ResultSet resultSet   = null;
		StringBuilder sql = new StringBuilder();
		sql.append(" select t.nome tipoUsuario ");
		sql.append("   from usuario u ");
		sql.append("       ,tipo_usuario t ");
		sql.append("  where u.id_tipo_usuario = t.id ");
		sql.append("    and u.login = ? ");
		sql.append("    and u.senha = ? ");
		
		try{
			
			//conecta ao banco de dados
			connection = ConnectionFactory.createConnectionToMySQL();
			preparedStatement = connection.prepareStatement(sql.toString());
			preparedStatement.setString(1, login);
			preparedStatement.setString(2, senha);
			//executa a consulta
			resultSet = preparedStatement.executeQuery();
			
			
			if (!resultSet.next()){
				resultado = null;
			}else{
				if (resultSet.getString("tipoUsuario").toUpperCase() == "ADMINISTRADOR"){
					resultado = TipoUsuario.ADMINISTRADOR;
				}else{
					resultado = TipoUsuario.CLIENTE;
				}
			}
			
		}catch (Exception e) {
			throw new DAOException(e);
		}finally{
			try{
				if(resultSet != null){
					resultSet.close();
				}
				if(preparedStatement != null){
					preparedStatement.close();
				}
				if(connection != null){
					connection.close();
				}
			}catch (Exception e) {
				throw new DAOException(e);
			}
		}
		
		return resultado;
	}
	
	public boolean existeUsuarioCom(String login) throws DAOException{
		boolean resultado = false;
		Connection connection = null;
		PreparedStatement preparedStatement   = null;
		ResultSet resultSet   = null;
		StringBuilder sql = new StringBuilder();
		sql.append(" select t.nome tipoUsuario ");
		sql.append("   from usuario u ");
		sql.append("       ,tipo_usuario t ");
		sql.append("  where u.id_tipo_usuario = t.id ");
		sql.append("    and u.login = ? ");
		
		try{
			
			//conecta ao banco de dados
			connection = ConnectionFactory.createConnectionToMySQL();
			preparedStatement = connection.prepareStatement(sql.toString());
			preparedStatement.setString(1, login);
			
			//executa a consulta
			resultSet = preparedStatement.executeQuery();
			
			if (!resultSet.next()){
				resultado = false;
			}else{
				resultado = true;
			}
			
		}catch (Exception e) {
			throw new DAOException(e);
		}finally{
			try{
				if(resultSet != null){
					resultSet.close();
				}
				if(preparedStatement != null){
					preparedStatement.close();
				}
				if(connection != null){
					connection.close();
				}
			}catch (Exception e) {
				throw new DAOException(e);
			}
		}
		
		return resultado;
	}

}
